<!-- * WanlShop - App全局模板配置
 * @author 深圳前海万联科技有限公司 <wanlshop@i36k.com> 
 * @link http://www.wanlshop.com
 * 
 * @本程序受法律保护，著作权©软著登记号2020SR0255711 < 未经版权所有权人书面许可，不能用于商业用途！>
 * 
 * @获取许可
 * 后台下载：https://www.fastadmin.net/store/wanlshop.html
 * 帮助文档：https://doc.fastadmin.net/wanlshop
 * 
 * @stress 本程序仅用作FastAdmin付费插件（WanlShop B2B2C商城）API使用，未经版权所有权人书面许可，不能自行用于商业用途
 * @creationtime  2019年9月10日12:52:20
 * @lasttime 2020年6月9日09:26:07
 * @version 1.0.0
 -->
<script>
	import Vue from 'vue';
	import permission from './common/permission';
	import wanlshop_config from './common/config/config';
	console.log(uni.getStorageSync('address'));
	export default {
		globalData: {
			adcode: uni.getStorageSync('address').ad_info ? uni.getStorageSync('address').ad_info.adcode : '510106',
			isPermissionAlertShow: false,
			permissionListener: null,
			level: 1,
		},
		onLoad(i) {
			console.log(i);
		},
	
		onLaunch() {
			this.addRouteInterceptor()
			this.getPhoneModel()
			this.globalData.level = wanlshop_config['level']
			// 检测客户端更新
			this.$store.dispatch('update/update');
			// 仅首次打开一次性加载数据
			this.$store.dispatch('common/init');
			// 同步购物车 本地 & 网络
			this.$store.dispatch('cart/get');
			// 同步 动态数据
			this.$store.dispatch('statistics/get');

			//只有在基座运行的情况下才可以进行打印看到
			// #ifdef APP-PLUS
			uni.getPushClientId({
				success: (res) => {
					let push_clientid = res.cid
				},
				fail(err) {
					console.log(err)
				}
			})
			// #endif
			uni.onPushMessage((res) => {
				console.log(res)
			})
			this.messagePush()
			if (!permission.isIOS) {
				// #ifdef APP
				this.watchPermission()
				// #endif
			}

		},
		methods: {
			getPhoneModel() { //获取手机信号
				try {
					const res = uni.getSystemInfoSync();
					const system = res.system.toLowerCase();
					let systemType = ''
					if (system.includes('harmonyos') || system.includes('hmos')) {
						systemType = '鸿蒙';
					} else if (system.includes('ios') || system.includes('iphone')) {
						systemType = '苹果';
					} else if (system.includes('android')) {
						systemType = '安卓';
					} else {
						systemType = '未知';
					}
					console.log('当前手机型号', systemType);
					let userinfo = uni.getStorageSync("wanlshop:user");
					userinfo.systemType = systemType
					uni.setStorageSync("wanlshop:user", userinfo);
					console.log(uni.getStorageSync("wanlshop:user"));
				} catch (e) {

					console.error(e);
				}
			},
			addRouteInterceptor() {
				// 增加一个返回键的拦截，从分享页面进入小程序，路由只有一级，这时点返回使其回到首页
				uni.addInterceptor("navigateBack", {
					invoke(e) {
						let routes = getCurrentPages()
						// console.log('navigateBack', routes)
						if (routes.length > 1) {
							return true
						} else {
							uni.reLaunch({
								url: '/pages/tabbarcompotant/index/landlord'
							})
							return false
						}
					}
				})
			},
			async messagePush() {
				var platform = uni.getSystemInfoSync().platform;
				//#ifdef APP-PLUS  
				/* 5+  push 消息推送 ps:使用:H5+的方式监听，实现推送*/
				plus.push.addEventListener("click", function(msgc) {
					console.log(msgc);
					//  && msgc.type == 'click'
					if (platform == 'ios') {
						//离线是payload为object , 在线时本地创建的为string(本地创建通知只能传string，否则无法传递payload)
						if (typeof msgc.payload == 'string') {
							console.log('string')
							var paymsg = JSON.parse(msgc.payload);
						} else {
							console.log('obj')
							var paymsg = msgc.payload;
						}
						console.log(paymsg)
						//执行跳转判断
					}
					//这里可以写跳转业务代码
				}, false);
				// 监听在线消息事件    
				plus.push.addEventListener("receive", function(msg) {
					//这里可以写跳转业务代码
					console.log(msg)
					if (msg.type == "receive") // 这里判断触发的来源，否则一直推送。  
					{
						if (platform == 'ios') {
							// console.log(msg.payload)
							plus.push.createMessage(
								msg.payload.content,
								//本地创建通知payload只能传string，否则无法成功传递（为null）
								JSON.stringify(msg.payload), {
									cover: false,
									title: msg.title,
								});
						}
					}
					if (platform == 'android') {
						//执行跳转判断
					}
				}, false);
				//#endif  
			},
			watchPermission() {
				this.permissionListener = uni.createRequestPermissionListener();
				this.permissionListener.onConfirm((e) => {
					this.isPermissionAlertShow = true
					console.log('alert')
					console.log(e)
					let name = "p";
					if (e[0] == 'android.permission.ACCESS_COARSE_LOCATION') {
						name = "pLocation"
					} else if (e[0] == 'android.permission.CAMERA') {
						name = 'pCamera'
					} else if (e[0] == 'android.permission.READ_MEDIA_IMAGES') {
						name = "pImage"
					}
					let obj = {
						k: name,
						v: true
					}
					this.$store.dispatch('m/showPermissionAlertShow', obj);
				});
				this.permissionListener.onComplete((e) => {
					let name = "p";
					if (e[0] == 'android.permission.ACCESS_COARSE_LOCATION') {
						name = "pLocation"
					} else if (e[0] == 'android.permission.CAMERA') {
						name = 'pCamera'
					} else if (e[0] == 'android.permission.READ_MEDIA_IMAGES') {
						name = "pImage"
					}
					this.isPermissionAlertShow = false
					let obj = {
						k: name,
						v: false
					}
					this.$store.dispatch('m/showPermissionAlertShow', obj);
				});
			},
		},
		onShow() {
			// 启动或重启即时通讯
			console.log('AppVue 页面');
			// this.$store.dispatch('chat/start');
			// #ifdef MP-WEIXIN
			// 小程序自動更新
			const updateManager = uni.getUpdateManager()
			// 请求完新版本信息的回调
			updateManager.onCheckForUpdate(res => {
				if (res.hasUpdate) {
					// 新版本下载成功
					updateManager.onUpdateReady(() => {
						uni.showModal({
							title: '更新提示',
							content: '新版本已经准备好，点击确定重启小程序',
							success(res) {
								if (res.confirm) {
									// 新的版本已经下载好，强制更新
									updateManager.applyUpdate()
								}
							}
						})
					})
				}
			})
			// 新版本下载失败
			updateManager.onUpdateFailed(res => {
				console.error(res)
			})
			// #endif

		},
		onHide() {
			// 断开即时通讯
			this.$store.dispatch('chat/close');
		}
	};
</script>

<style lang="scss">
	@keyframes progross1 {
		to {
			transform: rotate(360deg);
		}
	}

	@keyframes progross2 {
		to {
			transform: rotate(0deg);
		}
	}

	@import '@/static/style/gap.css';
	@import '@/static/style/flex.css';
	@import '@/static/style/myFontSize.css';
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';

	// 设置整个项目的背景色
	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}

	@import "./uni_modules/vk-uview-ui/index.scss";
	/* #ifndef APP-NVUE */
	@import 'static/style/colorui.css';
	@import 'static/style/icon.css';
	@import 'static/style/wanlshop.css';
	@import 'static/style/iconfont.css';

	/* #endif */
	// #ifndef APP-PLUS
	.landlord {

		//微信小程序
		::v-deep .uni-swiper-dots {
			bottom: 28rpx !important;
		}
	}

	.wlIcon-fenxiangcopy {
		display: none;
	}

	.wlIcon-fenxiangcopy {
		display: none !important;
	}

	.display-none {
		display: none !important;
	}

	.uni-app--showtabbar uni-page-wrapper::after {
		content: unset !important;
	}

	// #endif 
	.border-wrap {
		background: #ffffff;
		border-radius: 14rpx 14rpx 0 0;
		padding: 0 40rpx 0 40rpx;
	}

	.content-border-wrap {
		background-color: #ffffff;
		border-radius: 14rpx;
		padding: 24rpx;
	}

	.wrap {
		padding: 0 40rpx 0 40rpx;
	}

	.wrap-margin {
		margin: 0 40rpx 0 40rpx;
	}

	.like {
		width: 140rpx;
		height: 56rpx;
		border-radius: 66rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		&.not {
			background: linear-gradient(332deg, #FF4A33 0%, #FF8248 100%);
			color: #ffffff;
		}

		&.is {
			background-color: #F5F5F5;
			color: #808080;
		}
	}

	.main-btn {
		height: 84rpx;
		background: linear-gradient(160deg, #FFA64C 0%, #FD9328 56%, #FD8913 100%);
		box-shadow: 0rpx 8rpx 20rpx 0rpx rgba(254, 137, 19, 0.3);
		border-radius: 8rpx 8rpx 8rpx 8rpx;
		display: flex;
		justify-content: center;
		align-items: center;

		font-family: Source Han Sans, Source Han Sans;
		font-weight: 500;
		font-size: 30rpx;
		color: #FFFFFF;
		text-align: left;
		font-style: normal;
		text-transform: none;
	}

	.height-scroll {

		height: 100%;
		overflow: scroll;


	}
</style>